﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sodao.FastSocket.Server.Protocol
{


    /// <summary>
    /// 数组传输
    /// </summary>
    public sealed class ByteDataProtocol : IProtocol<Messaging.ByteDataMessage>
    {
        private Messaging.ByteDataMessage Parse_panel2(SocketBase.IConnection connection, ArraySegment<byte> buffer,
       int maxMessageSize, out int readlength)
        {
            try
            {
                readlength = 0;
                if (buffer.Count >= maxMessageSize)
                {
                    readlength = maxMessageSize;
                    return null;
                }
                else if (buffer.Count < 10)
                {
                    return null;
                }
                else
                {
                    for (int a = buffer.Offset, l = buffer.Offset + buffer.Count; a < l - 9; a++)
                    {
                        if (buffer.Array[a] == 0xf3)
                        {
                            ushort d = (ushort)((buffer.Array[a + 3 + 2 + 1] << 8) | buffer.Array[a + 4 + 2 + 1]);
                            byte end1 = buffer.Array[buffer.Offset + buffer.Count - 1];
                            if (d + 5 + 1 + 1 + 2 + 1 <= (buffer.Offset + buffer.Count - a ) && buffer.Array[a + d + 5 + 1 + 2 + 1] == 0xf4)
                            {
                                if (sumcheck(buffer.Array, a, d + 5 + 2 + 1) == buffer.Array[a + 4 + d + 1 + 2 + 1])
                                {
                                    readlength = a - buffer.Offset + d + 5 + 1 + 1 + 2 + 1;
                                    byte direct = buffer.Array[a + 1];
                                    byte m = buffer.Array[a + 1 + 2 + 1];        
                                    byte s = buffer.Array[a + 2 + 2 + 1];
                                    ushort sq = (ushort)(buffer.Array[a + 1 + 1] << 8 | buffer.Array[a + 2 + 1]);
                                    return new Messaging.ByteDataMessage(direct, sq, m, s, buffer.Array.Skip(a).Take(d + 5 + 1 + 1 + 2 + 1).ToArray());
                                }
                                else
                                {
                                    continue;
                                }
                            }
                            else
                            {
                                continue;
                            }
                        }
                        else
                        {
                            continue;
                        }
                    }
                    return null;
                }
            }
            catch (Exception e)
            {
                readlength = 0;
                throw e;
            }
        }
            /// <summary>
            /// parse
            /// </summary>
            /// <param name="connection"></param>
            /// <param name="buffer"></param>
            /// <param name="maxMessageSize"></param>
            /// <param name="readlength"></param>
            /// <returns></returns>
          public Messaging.ByteDataMessage Parse(SocketBase.IConnection connection, ArraySegment<byte> buffer,
         int maxMessageSize, out int readlength)
        {

            try
            {
                return  Parse_panel2(connection, buffer, maxMessageSize,out readlength);

            }
            catch (Exception e)
            {
                readlength = 0;
                throw e;
            }

#if false
            try
            {
                readlength = 0;
                if (buffer.Count >= maxMessageSize) 
                {
                    readlength = maxMessageSize;
                    return null;
                }
                else if(buffer.Count < 7)
                {
                    return null;
                }
                else
                {
                    for (int a = 0; a < buffer.Count - 6; a++)
                    {
                        if (buffer.Array[a] == 0xf3)
                        {
                            byte m = buffer.Array[a + 1];
                            byte s = buffer.Array[a + 2];
                            ushort d = (ushort)((buffer.Array[a + 3] << 8) | buffer.Array[a + 4]);
                            byte end1 = buffer.Array[buffer.Count-1];
                            if (d + 5 +1+1 <= buffer.Count - a  && buffer.Array[a+d + 5 + 1]==0xf4)
                            {                            
                                if (sumcheck(buffer.Array, a, d + 5) == buffer.Array[a + 4+ d+1])
                                {
                                    readlength = a  + d + 5 + 1+1;
                                    return new Messaging.ByteDataMessage(m, s, buffer.Array.Skip(a).Take(d + 5 + 1 + 1).ToArray());
                                }
                                else
                                {
                                    continue;
                                }
                            }else
                            {

                                continue;
                            }
                        }
                        else
                        {
                            continue;
                        }

                    }

                    return null;

                }
            }
            catch (Exception e)
            {
                readlength = 0;
                throw e;
            }

#endif

        }

        private byte sumcheck(byte[] buf, int offest, int len)
        {
            byte ret = 0x00;
            int b = 0;
            while (len-- > 0)
            {

                ret += buf[offest+b];
                b++;
            }
            return ret;
        }
    }
}
